/*
 * nlsync
 * Copyright (c) 2010 apechurin 
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace nlsync.XlsOld
{
    /// <summary>
    /// Implementation of the IStringInfo interface for a Xls based 
    /// implementation.
    /// THIS IMPLEMENTATION IS DEPRECATED. SEE Xls NAMESPACE.
    /// </summary>
    class XlsStringInfo : IStringInfo
    {
        #region Constructors

        /// <summary>
        /// Constructs a new string info. Used to represent a string which is
        /// already defined in Excel.
        /// </summary>
        /// <param name="cell">A cell that is inside the string info row.</param>
        public XlsStringInfo(Excel.Range cell)
        {
            mRange = cell.EntireRow.Cells;
            mIsNew = false;
        }

        /// <summary>
        /// Constructs a new string info. Used for newly added strings in Excel.
        /// </summary>
        /// <param name="cell">A cell that is inside the string info row.</param>
        public XlsStringInfo(Excel.Range cell, string source, string name)
        {
            mRange = cell.EntireRow.Cells;
            mIsNew = true;

            SetCellValue(SourceColumn, source);
            SetCellValue(NameColumn, name);
        }

        #endregion

        #region IStringInfo methods

        public string Name
        {
            get
            {
                return GetCellValue(NameColumn);
            }
        }

        public string DefaultValue
        {
            get
            {
                return GetCellValue(DefaultValueColumn);
            }
            set
            {
                SetCellValueAndUpdateColor(DefaultValueColumn, value);
            }
        }

        public string Value
        {
            get
            {
                return GetCellValue(ValueColumn);
            }
            set
            {
                SetCellValue(ValueColumn, value);
            }
        }

        public string Comment
        {
            get
            {
                return GetCellValue(CommentColumn);
            }
            set
            {
                SetCellValueAndUpdateColor(CommentColumn, value);
            }
        }

        #endregion

        #region Internal methods

        /// <summary>
        /// Gets a string range.
        /// </summary>
        internal Excel.Range Range
        {
            get
            {
                return mRange;
            }
        }

        /// <summary>
        /// Removes the string info. The object cannot be used anymore after 
        /// the call.
        /// </summary>
        internal void Remove()
        {
            mRange.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
            mRange = null;
        }

        /// <summary>
        /// Gets the source (aka bundle name).
        /// </summary>
        internal string Source
        {
            get
            {
                return GetCellValue(SourceColumn);
            }
        }

        #endregion

        #region Implementation

        /// <summary>
        /// Gets the value of the specified column.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <returns>The value of the cell.</returns>
        private string GetCellValue(int column)
        {
            Excel.Range c = (Excel.Range)mRange.get_Item(1, column);
            object t = c.Value2;
            return t != null ? t.ToString() : "";
        }

        /// <summary>
        /// Sets the value of the specified column.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <param name="value">The new value.</param>
        /// <returns>The range.</returns>
        private Excel.Range SetCellValue(int column, string value)
        {
            Excel.Range c = (Excel.Range)mRange.get_Item(1, column);
            c.Value2 = value;
            return c;
        }

        /// <summary>
        /// Sets the value of the specified column and also updates its color.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <param name="value">The new value.</param>
        /// <returns>The range.</returns>
        private Excel.Range SetCellValueAndUpdateColor(int column, string value)
        {
            Excel.Range c = SetCellValue(column, value);

            // update cell color
            int color = mIsNew ? RGB(255, 255, 0) : RGB(255, 0, 0);
            c.Interior.Color = color;

            return c;
        }

        /// <summary>
        /// Gets the RGB color value.
        /// </summary>
        private int RGB(int r, int g, int b)
        {
            return (b << 16 | g << 8 | r);
        }

        #endregion

        #region Data fields

        private Excel.Range mRange; // the entire row range
        bool mIsNew; // is it a new row or an old one

        // columns
        private static readonly int SourceColumn = 1;
        private static readonly int NameColumn = 2;
        private static readonly int DefaultValueColumn = 3;
        private static readonly int ValueColumn = 4;
        private static readonly int CommentColumn = 5;

        #endregion
    }
}
